#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
import threading as th
import multiprocessing as mp


def myprint(obj, end='\n'):
    sys.stdout.write(str(obj) + end)


def pfunc(q):
    pid = os.getpid()
    for i in range(50):
        it = 'subprocess %d::%d' % (pid, i)
        q.put(it)


def thfunc(q):
    while True:
        bf = q.get()
        if bf is None:
            break
        myprint(bf)


def main():
    q = mp.Queue()
    ps = [mp.Process(target=pfunc, args=(q, )) for i in range(5)]
    ths = th.Thread(target=thfunc, args=(q, ))
    for p in ps:
        p.start()
    ths.start()

    for p in ps:
        p.join()
    q.put(None)
    ths.join()


if __name__ == '__main__':
    main()
    myprint('done')
